package v3.chapter3.joinMoreTest;

public class Run {

	public static void main(String[] args) {
		// 1
//		ThreadB b = new ThreadB();
//		ThreadA a = new ThreadA(b);
//		a.start();
//		b.start();
//		System.out.println("   main end=" + System.currentTimeMillis());
		
		try {
			ThreadB b = new ThreadB();
			ThreadA a = new ThreadA(b);
			a.start();
			b.start();
			b.join(2000);
			System.out.println("                    main end " + System.currentTimeMillis());
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}

}

class ThreadB extends Thread {
	@Override
	synchronized public void run() {
		try {
			System.out.println("begin B ThreadName=" + Thread.currentThread().getName() + "  " + System.currentTimeMillis());
			Thread.sleep(5000);
			System.out.println("  end B ThreadName=" + Thread.currentThread().getName() + "  " + System.currentTimeMillis());
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class ThreadA extends Thread {
	private ThreadB b;
	public ThreadA(ThreadB b) {
		super();
		this.b = b;
	}

	@Override
	public void run() {
		try {
			synchronized (b) {
				System.out.println("begin A ThreadName=" + Thread.currentThread().getName() + "  " + System.currentTimeMillis());
				Thread.sleep(5000);
				System.out.println("  end A ThreadName=" + Thread.currentThread().getName() + "  " + System.currentTimeMillis());
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
